package com.fm.financemanage.service;

import com.fm.financemanage.mapper.UserMapper;
import com.fm.financemanage.model.po.SimCard;
import com.fm.financemanage.model.po.User;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.simcard.SimCardOpeartorVo;
import com.fm.financemanage.model.vo.user.UserListVo;
import com.fm.financemanage.utils.MessageDigestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public PageVo<List<UserListVo>> getList(String name, Integer state, Integer page, Integer limit) {
        return new PageVo<List<UserListVo>>(userMapper.getList(name, state, (page-1)*limit, limit), userMapper.count(name, state));
    }

    public void operator(User user) {

        if (user.getId() == null) {
            user.setUuid(UUID.randomUUID().toString().replace("-", ""));
            user.setCreateTime(LocalDateTime.now());
            user.setUpdateTime(LocalDateTime.now());
            userMapper.insertSelective(user);
        } else {
            user.setUpdateTime(LocalDateTime.now());
            userMapper.updateByPrimaryKeySelective(user);
        }
    }

    public void removeByUuid(String uuid) {
        userMapper.removeByUuid(uuid);
    }

    public void resetPassword(Integer userId, String password) {
        userMapper.resetPassword(userId, MessageDigestUtil.encryptMD5(password.getBytes()));
    }
}
